package rxtxsample;


import gnu.io.*;

import java.io.*;
import java.util.*;

import javax.print.DocFlavor.STRING;

public class SerialComm implements SerialPortEventListener{
	
	SerialPort serialPort; 
	InputStream inputStream;
	public OutputStream outputStream;
	public int isOpen = 0;
	
	public Vector<String> getPortList() {
		Enumeration<CommPortIdentifier> portList;
		Vector<String> portVect = new Vector<String>();
		portList = CommPortIdentifier.getPortIdentifiers();

		CommPortIdentifier portId;
		while (portList.hasMoreElements()) {
			portId = (CommPortIdentifier) portList.nextElement();
			if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
				portVect.add(portId.getName());
			}
		}
		System.out.println(portVect.size() + " gefundene Ports:");
		for (int i = 0; i < portVect.size(); i++) {
			System.out.println(i + "  : " + (String) portVect.elementAt(i));
		}
		
		return portVect;
	} // get Port list
	
	public void closeSio() {
            inputStream = null;
            outputStream = null;
            serialPort.close();
        }

	public boolean openSio(int aPortListNumber, int aBaud) {
 		CommPortIdentifier portId = null;
		Enumeration portList;
		portList = CommPortIdentifier.getPortIdentifiers();
		while (portList.hasMoreElements()) {
			portId = (CommPortIdentifier) portList.nextElement();
			if (aPortListNumber == 0) break;
			aPortListNumber--;
			} 
		System.out.println("Oeffnen von Port Nr "+aPortListNumber);
		if (portId == null) {System.out.println("Port nicht gefunden");return false;};
// System.out.println("pos1");
		try {
			serialPort = (SerialPort) portId.open("Serial_Test", 1000);
		} catch (PortInUseException e) {System.out.println("Port in Use: "+e.toString()); }
// tem.out.println("pos2");
		if (serialPort != null)
		try {
			inputStream = serialPort.getInputStream();
		} catch (IOException e) {System.out.println("GetInpStream: "+e.toString()); }
		if (inputStream != null)
		try {
			serialPort.addEventListener(this);
		} catch (TooManyListenersException e) {System.out.println("ToMAnyListeners: "+e.toString()); }
		if (serialPort != null) serialPort.notifyOnDataAvailable(true);
		if (serialPort != null) try {
			serialPort.setSerialPortParams(aBaud, SerialPort.DATABITS_8, 
						   SerialPort.STOPBITS_1, 
						   SerialPort.PARITY_NONE);
		} catch (UnsupportedCommOperationException e) {System.out.println("Uns.Comm.Op.Ex.: "+e.toString()); }
// System.out.println("pos3");

		if (serialPort != null) try {
			outputStream = serialPort.getOutputStream();
		} catch (IOException e) {System.out.println("IOExcp: "+e.toString()); }
// System.out.println("pos4");
                return (inputStream != null);

/*		try {
			serialPort.notifyOnOutputEmpty(true);
		} catch (Exception e) {
			System.out.println("Die event notification konnte nicht initialisiert werden");
			System.out.println(e.toString());
			}
*/
//			System.exit(-1);
// System.out.println("pos5");		
	}

	public void serialEvent(SerialPortEvent event) {

		switch (event.getEventType()) {
		case SerialPortEvent.BI:
		case SerialPortEvent.OE:
		case SerialPortEvent.FE:
		case SerialPortEvent.PE:
		case SerialPortEvent.CD:
		case SerialPortEvent.CTS:
		case SerialPortEvent.DSR:
		case SerialPortEvent.RI:
		case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
			break;
		case SerialPortEvent.DATA_AVAILABLE:

/*			byte[] readBuffer = new byte[2];
			try {
			   int numBytes = 0;
				while (inputStream.available() > 0) {
					numBytes = inputStream.read(readBuffer);
				} 
//				System.out.println("Read: "+new String(readBuffer, numBytes));
int i;
for(i=0;i<numBytes;i++) 	System.out.println(" "+readBuffer[i]);
				System.out.println("numBytes: "+numBytes);
								
			} catch (IOException e) {}
*/	
		break;
		} // switch
	}  // method serialEvent 
        
    public void myWrite(String s) {
                try {

                    int len = s.length();
                    System.out.print("Sende "+len+" Zeichen: ");
                    for(int i = 0; i<len;i++) {
			int c = s.charAt(i);
                        System.out.printf("%02X ", c);
                        outputStream.write(s.charAt(i));
                    }
                    outputStream.write('\n');
                    System.out.println();
                    
                } catch (IOException e) {
                    System.err.println("IOException: "  + e.getMessage());        
                }  // catch
            }

 public void myWriteInt(int anI) {
               try {
 			outputStream.write(anI);
                    }
 catch (IOException e) {
                    System.err.println("IOException: "  + e.getMessage());
                }  // catch
            }

// ------ must implement this method ...
	public int readSio() throws java.io.IOException {
		byte[] readBuffer = new byte[1];
		int c;
		int numBytes = 0;
//		try {
                if (inputStream != null)
		   if (inputStream.available() > 0) {
			numBytes = inputStream.read(readBuffer);
			} 
//		catch (IOException e) {}
		if (numBytes ==1) {
			c = (readBuffer[0] >= 0 ? readBuffer[0] : readBuffer[0] +256);
 			return c;
		} 
		else return -1;		
	}

	String getLine(int tio) {
		String s = "";
		boolean emptyflag = true;
		int c = 0;
		while (tio > 0) {
		  try {c = readSio(); } catch (IOException e) {};
		  if (c != -1) {
			  if (c>= 32) {
				  s += (char) c;
				  emptyflag = false;
				  }
			  else {
				  if (!emptyflag) {
				   if (s.charAt(0) != '#') {
					   System.out.println("s -> "+ s);
					   return s;  
				   }
				   System.out.println(s);
				   emptyflag = true;
				   s = "";
				  } // emptyflag
			  } // else c>= 32
		  } // c!=-1
		  else {
			  try {Thread.sleep(10);}
			  catch (InterruptedException e) {};
			  tio -= 10;
		  }
		} // while tio
		return null;
	} // method getLine

	int waitfor(String[] a, int tio) {
		while (true) {
			String s = getLine(tio);
			if (s == null) return -1;
			for(int i = 0; i < a.length; i++) {
				if (a[i].equals(s)) 
					return i;
			}
		} // while notfound
	} // waitfor
	
	void println(String s) {
		//s += 0x0d;
        try {outputStream.write( s.getBytes());} catch(Exception e) {};
	}
}
